package com.longma.server.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.longma.server.entity.InspectionPlan;
import com.longma.server.pojo.UserResponseEntity;
import com.longma.server.pojo.dto.*;
import com.longma.server.service.IInspectionPlanService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */
@RestController
@RequestMapping("/api/inspection-plan")
public class InspectionPlanController {
    @Autowired
    IInspectionPlanService iInspectionPlanService;

    @ApiOperation(value = "新增巡視計劃")
    @PostMapping("/add")
    public UserResponseEntity<InspectionPlan> addInspectionPlan(@Valid @RequestBody InspectionPlanDTO addRequestDTO){
        return UserResponseEntity.success(iInspectionPlanService.addInspectionPlan(addRequestDTO));
    }

    @ApiOperation(value = "根据巡視計劃id删除巡視計劃")
    @PostMapping("/del")
    public UserResponseEntity<Boolean> deleteInspectionPlanById(@NotNull(message = "巡視計劃id不能为空")  @RequestParam Integer id){
        return UserResponseEntity.success(iInspectionPlanService.deleteInspectionPlanById(id));
    }

    @ApiOperation(value = "根据巡視計劃id列表删除巡視計劃")
    @PostMapping("/dellist")
    public UserResponseEntity<Boolean> deleteInspectionPlanByIds(@RequestParam(name="ids") @Size(min = 1,message = "巡視計劃id列表不能为空")
                                                                           List<@Valid @NotNull(message = "巡視計劃id不能为空") Integer> ids){
        return UserResponseEntity.success(iInspectionPlanService.deleteInspectionPlanByIds(ids));
    }

    @ApiOperation(value = "更新巡視計劃")
    @PostMapping("/update")
    public UserResponseEntity<InspectionPlan> updateInspectionPlanSetting(@Valid @RequestBody InspectionPlanUpdateDTO updateRequestDTO){
        return UserResponseEntity.success(iInspectionPlanService.updateInspectionPlan(updateRequestDTO));
    }

    @ApiOperation(value = "获取所有巡視計劃(分页)")
    @PostMapping("/list")
    public UserResponseEntity<IPage<InspectionPlan>> getInspectionPlanByPage(@ApiParam(value = "分页参数",required = true) @Valid @RequestBody PageRequestDTO pageRequestDTO){
        return UserResponseEntity.success(iInspectionPlanService.getInspectionPlanByPage(pageRequestDTO.getPageNum(),
                pageRequestDTO.getPageSize(),pageRequestDTO.getAll(),pageRequestDTO.getConditions()));
    }
}

